package com.GLM.Tools.Utils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.GLM.Viewer.R;

public class ViewsUtil {

	public static void popSoftInput(final Context c) {
		new Timer().schedule(new TimerTask() {
			public void run() {
				InputMethodManager imm = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			}

		}, 100);
	}

	public static StateListDrawable getSelector(final Context c) {
		StateListDrawable selector = new StateListDrawable();

		Drawable select = c.getResources().getDrawable(R.drawable.dot_orange);
		Drawable enable = c.getResources().getDrawable(R.drawable.dot_white);
		Drawable pressed = c.getResources().getDrawable(R.drawable.dot_green);

		selector.addState(new int[] { android.R.attr.state_pressed }, pressed);
		selector.addState(new int[] { android.R.attr.state_selected }, select);
		selector.addState(new int[] { android.R.attr.state_enabled }, enable);
		return selector;
	}

	public static SpannableString getRegisterButtonSpannableString(String line1, String line2) {
		String line = line1 + "\n" + line2;
		int startSpan = line.indexOf("\n");
		SpannableString spanText = new SpannableString(line);
		spanText.setSpan(new ForegroundColorSpan(R.color.newparty_view_textcolor), 0, line1.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		spanText.setSpan(new RelativeSizeSpan(2.5f), 0, line1.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		spanText.setSpan(new ForegroundColorSpan(Color.BLACK), startSpan, line.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		return spanText;
	}

	public static void showErrorTip(final Context c, int errMsgId) {
		Toast toast = Toast.makeText(c.getApplicationContext(), null, Toast.LENGTH_SHORT);
		toast.setText(errMsgId);
		toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 20);
		toast.show();
	}

	public static void sleep500ms() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static boolean validateEmail(String email) {
		String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern pattern = Pattern.compile(EMAIL_PATTERN);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();

	}

	public static void navigateTo(Activity thisActivity, Intent intent) {
		thisActivity.startActivity(intent);
		thisActivity.finish();
	}

}
